package com.cloudptt.wp.lock.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.cloudptt.wp.R;
import com.cloudptt.wp.lock.DetailActivity;
import com.cloudptt.wp.lock.receiver.LockScreenReceiver;


public class LockScreenService extends Service {
    private LockScreenReceiver mReceiver;
    private IntentFilter mIntentFilter = new IntentFilter();
    private boolean isNotiShow = false;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //动态注册
        mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        mIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
        mIntentFilter.addAction(Intent.ACTION_TIME_TICK);

        mIntentFilter.setPriority(Integer.MAX_VALUE);
        if (null == mReceiver) {
            mReceiver = new LockScreenReceiver();
            mIntentFilter.setPriority(Integer.MAX_VALUE);
            registerReceiver(mReceiver, mIntentFilter);

            buildNotification();
            Toast.makeText(getApplicationContext(), "开启成功", Toast.LENGTH_LONG).show();
        }

        return START_STICKY;
    }

    /**
     * 通知栏显示
     */
    private void buildNotification() {
        if (!isNotiShow){ //避免多次显示
//            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//            Intent intent = new Intent(this, DetailActivity.class);
//            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//            Notification notification = new NotificationCompat.Builder(this)
//                    .setTicker("APP正在运行")
//                    .setAutoCancel(false)
//                    .setContentTitle("APP正在运行")
//                    .setContentText("运行中")
//                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ptticon))
//                    .setSmallIcon(R.drawable.ptticon)
//                    .setContentIntent(pendingIntent)
//                    .build();
//            manager.notify(1, notification);
//
//            startForeground(0x11, notification);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        "channel_id", "Foreground Service",
                        NotificationManager.IMPORTANCE_LOW
                );
                getSystemService(NotificationManager.class).createNotificationChannel(channel);
            }

            // 构建通知（必须设置小图标、标题和内容）
            Notification notification = new NotificationCompat.Builder(this, "channel_id")
                    .setContentTitle("服务运行中")
                    .setContentText("正在后台执行任务...")
                    .setSmallIcon(R.drawable.ptticon)
                    .build();

            // 调用 startForeground()（ID 必须 > 0）
            startForeground(1, notification);

            isNotiShow = true;
        }
    }

    @Override
    public void onDestroy() {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
        super.onDestroy();
    }
}